from django.shortcuts import render, redirect, reverse
from django.http.response import JsonResponse
import string
import random
from django.core.mail import send_mail
from .models import CaptchaModel
from django.views.decorators.http import require_http_methods
from .forms import signupForm, loginForm
from django.contrib.auth import get_user_model, login, logout 
from django.contrib.auth.models import User

# Create your views here.

User = get_user_model()

@require_http_methods(['GET', 'POST'])
def signup(request):
    if request.method == 'GET':
        return render(request, 'signup.html')
    else:
        form = signupForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data.get('email')
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')
            # User(email = email, ...)不会对密码加密
            User.objects.create_user(email = email, username = username, password = password)
            return redirect(reverse("zlauth:login"))
        else:
            print(form.errors)
            return redirect(reverse("zlauth:signup"))
            # 下面这种方式可以显示错误信息
            # return render(request, 'signup.html', context = {'form': form})



def send_mail_captcha(request):
    email = request.GET.get('email')
    if not email:
        return JsonResponse({'code': 400, 'message': '必须传递邮箱'})
    captcha = ''.join(random.sample(string.digits, 4))
    # 先判断邮箱是否已经创建,这里不要使用create，有的话就赋值default
    CaptchaModel.objects.update_or_create(email = email, defaults = {'captcha': captcha})
    
    send_mail('知了博客验证码', message = f'您的注册验证码是:{captcha}', recipient_list = [email], from_email = None)
    return JsonResponse({'code': 200, 'message': '邮箱验证码发送成功'})

@require_http_methods(['GET', 'POST'])
def zllogin(request):
    if request.method == 'GET':
        return render(request, 'login.html')
    else:
        form = loginForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data.get('email')
            password = form.cleaned_data.get('password')
            remember = form.cleaned_data.get('remember')
            user = User.objects.filter(email = email).first()
            if user and user.check_password(password):
                login(request, user)
                if not remember:
                    # 如果么有设置记住我，浏览器session过期时间设置为0
                    request.session.set_expiry(0)
                return redirect('/')
            else:
                print('邮箱或者密码错误')
                # form.add_error('email', '邮箱或者密码错误')
                # return  render(request, 'login.html', context = {'form': form})
                return redirect(reverse("zlauth:login"))
        else:
            print(form.errors)
            return redirect(reverse("zlauth:login"))
        
def zllogout(request):
    # request里有当前用户的信息
    logout(request)
    return redirect("/")